/**
 * Created by Administrator on 2017/3/31.
 */
$(document).ready(function () {
    pur.initial();
});
var pur = {
    initial: function () {

        $("#source_file").change(function () {
            var arrs = $(this).val().split('\\');
            var filename = arrs[arrs.length - 1];
            $("#txtFileUpload").val(filename);
        });

        $("#upPolicy").click(function () {
            pur.upPolicy();
        });
    },
    upPolicy: function () {
        //确认导入
        var loading = $("#upPolicy").button('loading');

        var dataValue = new FormData();
        $.each($('#source_file')[0].files, function (i, file) {
            dataValue.append('upload_file', file);
        });
        if (dataValue.length == 0) {
            $("#warning").html("没有可上传文件！");
            $(".alert-warning").show(400, 'swing', function () {
                var setTime = window.setTimeout(function () {
                    $(".alert-warning").hide();
                    window.clearTimeout(setTime);
                }, 3000);
            });
            loading.button('reset');
            return;
        }

        $.ajax({
            url: '/hongtu/v1/upload',
            type: 'POST',
            data: dataValue,
            cache: false,
            contentType: false,    //不可缺
            processData: false,    //不可缺
            success: function (data) {
                console.log("data:" + data.message);
                console.log("data:" + data.code);
                loading.button('reset');
                if (3 == data.code) {
                    $("#note").html(data.message);
                    $(".alert-info").show(400, 'swing', function () {
                        var setTime = window.setTimeout(function () {
                            $(".alert-info").hide();
                            window.clearTimeout(setTime);
                        }, 3000);
                    });
                } else {
                    $("#warning").html(data.message);
                    $(".alert-warning").show(400, 'swing', function () {
                        var setTime = window.setTimeout(function () {
                            $(".alert-warning").hide();
                            window.clearTimeout(setTime);
                        }, 3000);
                    });
                }
            },
            error: function (e) {
                loading.button('reset');
                $("#warning").html("上传出错！");
                $(".alert-warning").show(400, 'swing', function () {
                    var setTime = window.setTimeout(function () {
                        $(".alert-warning").hide();
                        window.clearTimeout(setTime);
                    }, 3000);
                });
            }
        });
    }
}